package com.lili.goods.service.impl;

import com.lili.goods.mapper.GoodsCategoryMapper;
import com.lili.goods.service.GoodsCategoryService;
import com.lili.model.goods.GoodsCategory;
import com.lili.model.goods.GoodsUnit;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

@Service
public class GoodsCategoryServiceImpl implements GoodsCategoryService {
    @Autowired
    GoodsCategoryMapper goodsCategoryMapper;
    @Override
    public int saveCategory(GoodsCategory goodsCategory) {
        return goodsCategoryMapper.insert(goodsCategory);
    }

    @Override
    public List<Map> queryCategoryTree() {
        return goodsCategoryMapper.queryCategoryTree();
    }

    /**
     * 修改分类的禁用状态
     * @param id
     * @param status
     * @return
     */
    @Override
    public int modifyStatus(Long id, int status) {
       return modifyStatusById(id,status);
    }

    /**
     * 递归修改状态
     * @param id
     * @param status
     * @return
     */
    public int modifyStatusById(Long id, int status){
        //先修改
        int rs = goodsCategoryMapper.modifyStatus(id,status);
        //遍历查询是否有子类
        List<GoodsCategory> list = goodsCategoryMapper.queryByPid(id);
        if(list==null){
            return rs;
        }
        for (int i = 0, len = list.size();i< list.size();i++){
            rs = modifyStatusById(list.get(i).getId(),status);
        }
        return rs;
    }

    /**
     * 更新分类信息
     * @param goodsCategory
     * @return
     */
    @Override
    public int updateCategory(GoodsCategory goodsCategory) {
        return goodsCategoryMapper.updateCategory(goodsCategory);
    }

    /**
     * 根据id删除分类信息
     * @param id
     * @return
     */
    @Override
    public int deleteCategory(Long id) {
        List<GoodsCategory> list = goodsCategoryMapper.queryByPid(id);
        if(null != list){
            return 0;
        }
        return goodsCategoryMapper.deleteCategory(id);
    }
}
